﻿using Medicine.Entity;
using Medicine.Service;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Medicine
{
    public partial class AdminForm : Form
    {
        private User u;
        private MedicineServie ms=new MedicineServie();
        public AdminForm(User u)
        {
            this.u = u;
            InitializeComponent();
            flush();
            this.label3.Text = u.Name;
        }
        //刷新
        public void flush()
        {
            List<Medicin> med_list = ms.queryMedAll1();
            this.listView1.Items.Clear();
            foreach (Medicin med in med_list)//遍历查询出来的数据
            {
                ListViewItem lv = new ListViewItem();
                lv.Text = med.Id.ToString();//设置第一行显示的数据
                //绑定剩余列的数据
                lv.SubItems.Add(med.Name);
                lv.SubItems.Add(med.Price.ToString());
                if (med.Ison == 1)
                    lv.SubItems.Add("是");
                else
                    lv.SubItems.Add("否");
                lv.SubItems.Add(med.Information);

                //一定记得行数据创建完毕后添加到列表中
                listView1.Items.Add(lv);
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            CheckRecord cr = new CheckRecord();
            cr.Show();
        }
        //登出
        private void button2_Click(object sender, EventArgs e)
        {
            Form1 f = new Form1();
            this.Close();
            f.Show();
        }

        private void AdminForm_Load(object sender, EventArgs e)
        {
            label4.Text = System.DateTime.Now.ToString();
            //timer1.Interval = 1;
            timer1.Start();
        }
        //删除药品
        private void button4_Click(object sender, EventArgs e)
        {  
            if (this.listView1.CheckedItems.Count != 0) 
            {
                foreach (ListViewItem lv in this.listView1.CheckedItems)
                {
                    Medicin m = new Medicin();
                    m.Id=int.Parse(lv.SubItems[0].Text);
                    m.Name = lv.SubItems[1].Text;
                    m.Price = float.Parse(lv.SubItems[2].Text);
                    m.Information = lv.SubItems[4].Text;
                    if (lv.SubItems[3].Text.Equals("是"))
                        m.Ison = 1;
                    else
                        m.Ison = 0;
                    ms.delete(m);
                }
                flush();
            }
        }
        //修改药品
        private void button5_Click(object sender, EventArgs e)
        {
            if (this.listView1.CheckedItems.Count == 1)
            {
                foreach (ListViewItem lv in this.listView1.CheckedItems)
                {
                    Medicin m = new Medicin();
                    m.Id = int.Parse(lv.SubItems[0].Text);
                    m.Name = lv.SubItems[1].Text;
                    m.Price = float.Parse(lv.SubItems[2].Text);
                    m.Information = lv.SubItems[4].Text;
                    if (lv.SubItems[3].Text.Equals("是"))
                        m.Ison = 1;
                    else
                        m.Ison = 0;
                    View.UpdateMedicine um = new View.UpdateMedicine(m);
                    um.Show();
                }
            }
            else
            {
                MessageBox.Show("只能选择一个");
            }
        }
        //添加药品
        private void button6_Click(object sender, EventArgs e)
        {
            AddMedicine am = new AddMedicine();
            am.Show();
            
        }

        private void button3_Click(object sender, EventArgs e)
        {
            flush();
        }
        
        private void timer1_Tick_1(object sender, EventArgs e)
        {
            label4.Text = System.DateTime.Now.ToString();
        }
    }
}
